JObj.use("ui");

JObj.UI.JDrag = {};

/*
2147483647
模拟拖动时，最大的问题是当当前有内容被选中，这时的模拟就会变成真的拖动，所以，要把选中的内容取消。
*/

//=======================================================================================

(function($){
	var mAWidth = 200;
	var mAHeight = 200;

	var lastObj = null;

	var mArea = JObj.$c("DIV");
    var pPos = {x:0,y:0}

    document.body.insertBefore(mArea,document.body.lastChild);
	//有颜色和无颜色的运行表现是不一样的！为了看见鼠标下面的东东，设置透明度为0
    JObj.css(mArea,{width:mAWidth + 'px',height:mAHeight + 'px',position:'absolute',backgroundColor:'#000000',alpha:10,display:'none',zIndex:JObj.UI.JLayerManager.MAX_ZINDEX});
	var x,y;

	var changeZIndex = function(obj){
		if(lastObj){
            JObj.$(lastObj).style.zIndex = JObj.UI.JLayerManager.MAX_ZINDEX - 2;
			JObj.$(obj).style.zIndex = JObj.UI.JLayerManager.MAX_ZINDEX - 1;
		}
		lastObj = obj;
	}

	var mArea_mouseup = function(){
        JObj.Style.css(mArea,{width:1,height:1,display:"none"})
	}

    var noselect = function(){
          try{
			if(document.selection){//IE ,Opera
				if(document.selection.empty)
					document.selection.empty();//IE
				else{//Opera
					document.selection = null;
				}
			}else if(window.getSelection){//FF,Safari
				window.getSelection().removeAllRanges();
			}
		}catch(e){}
    }    

	var mArea_mousemove = function(obj,rule){
        noselect();
        rule = rule || {};
		var evt = JObj.getEvent();
		var oPos = JObj.UI.JPos.getAbsPos(obj);
		var mPos = JObj.UI.JPos.getEventPos(evt);

        var tx = ty = 0;

        JObj.Style.css(mArea,{left:mPos.x - mAWidth / 2,top:mPos.y - mAHeight / 2});

        if(!rule.lockX){
            tx = oPos.x + mPos.x - x - pPos.x;
            if( !isNaN(rule.minX) && tx < rule.minX) tx = rule.minX;
            if( !isNaN(rule.maxX) && tx > rule.maxX) tx = rule.maxX;
            obj.style.left = tx + "px";
        }

        if(!rule.lockY){
            ty = oPos.y + mPos.y - y - pPos.y
            if( !isNaN(rule.minY) && ty < rule.minY) ty = rule.minY;
            if( !isNaN(rule.maxY) && ty > rule.maxY) ty = rule.maxY;
            obj.style.top = ty + 'px';
        }

        JObj.isFunction(rule.onDrag) && rule.onDrag(obj,tx,ty);

        x = mPos.x;
        y = mPos.y;
	}

   /*
   rule = {
    lockX:true,//X轴的位置不改变
    lockY:false,//Y轴的位置可改变
    minX:0,//
    minY:100,
    maxX:100,
    maxY:100
   }
    */

	var dragArea_mousedown = function(obj,dragArea,rule){

        noselect();

		var evt = JObj.getEvent();
		var mPos = JObj.UI.JPos.getEventPos(evt);
        x = mPos.x;
        y = mPos.y;
        /*
        如果 offsetParent 是 body的话，由于 body 可能有 margin,所以把 body也不算在内
        a 由于 obj 的 position 是 absolute ，如果 obj 的offsetParent 的 position 是 absolute 的话，left,top 这些值是相对于 offsetParent来说的。
         而getAbsPos 取得的是绝对位置，不是 相对于 offsetParent 的 
         */
        obj.offsetParent && obj.offsetParent != document.body && (pPos = JObj.UI.JPos.getAbsPos(obj.offsetParent));

        JObj.Style.css(mArea,{width:mAWidth,height:mAHeight,left:mPos.x - mAWidth / 2,top:mPos.y - mAHeight / 2,cursor:"move",display:''});

		mArea.onmousemove = JObj.doFunction(mArea_mousemove,obj,/*dragArea,*/rule);
		mArea.onmouseout = mArea.onmouseup = mArea_mouseup;
	}

    /**
     * 
     * @param obj
     * @param dragAble boolean 是否可拖动,default true
     * @param rule
     */
    $.setDrag = function(obj,dragAble,rule){
        obj = JObj.$(obj);
        obj.style.position = "absolute";

		var dragArea = JObj.$(arguments[1]) || obj;

		if(dragAble !== false){
			//dragArea.onmousedown = JObj.doFunction(dragArea_mousedown,obj,dragArea);
			//obj.onmousedown = JObj.doFunction(changeZIndex,obj);

            JObj.addEvent(dragArea,'mousedown',JObj.doFunction(dragArea_mousedown,obj,dragArea,rule));
            JObj.addEvent(obj,'mousedown',JObj.doFunction(changeZIndex,obj,rule));
        }else{
			dragArea.onmousedown = "";
			obj.onmousedown = "";
		}
	}
    
})(JObj.UI.JDrag)